package com.tutu.user.security;

import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.tutu.common.GlobalConstants;
import com.tutu.common.util.LogAction;
import com.tutu.common.util.SysAuditModelType;
import com.tutu.user.entity.SysMenuInfo;
import com.tutu.user.entity.SysUser;
import com.tutu.user.service.ISysMenuInfoService;
import com.tutu.user.service.ISysUserService;

/**
 *  登录判断并赋于权限
 *  @author tutu
 *	@date 2013-11-19
 *	@company cjcc
 *	@version meoa 1.0
 */
@LogAction(ownermodel=SysAuditModelType.LOGIN_MANAGEMENT)
@Service("myUserDetailService")
public class MyUserDetailService implements UserDetailsService {
	
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private ISysUserService userService;
	@Resource
	private ISysMenuInfoService sysMenuInfoService;
	@Resource(name="messageSource")
	private MessageSource messageSource;
	
	@LogAction(description="用户登录", operateType="登录")
	@Override
	public UserDetails loadUserByUsername(String s)
			throws UsernameNotFoundException {
		SysUser sysUser = null;
		try {
			sysUser = this.userService.getUserByName(s);
			if(null==sysUser)
				throw new UsernameNotFoundException("未能找到用户");
		} catch (Exception e) {
			logger.error("验证用户失败", e);
			throw new UsernameNotFoundException(this.messageSource.getMessage("LOGON_VERIFY_FAIL", null, "验证用户失败", null), e);
		}
		String superUser = this.messageSource.getMessage("SUPER_USER", null, "superUser", null);
		if(!superUser.equals(s)){
			if(GlobalConstants.FLAG_DISENABLE.equals(sysUser.getAliveFlag()) ) {
				throw new UsernameNotFoundException(this.messageSource.getMessage("LOGON_USER_NAME_INVALID", null, "输入的用户名无效!", null));
			}
			if(GlobalConstants.FLAG_DISENABLE.equals(sysUser.getLockFlag())) {
				throw new UsernameNotFoundException(this.messageSource.getMessage("LOGON_USER_LOCK", null, "该用户已锁定!", null));
			}
		}
//		Calendar calendar = Calendar.getInstance();
//		sysUserLoginInfo.setCurrentLoginDate(calendar.getTime());
//		sysUserLoginInfo.setLastLoginDate(calendar.getTime());
//		用户权限读取
		Set<SysMenuInfo> sysMenuInfos;
		try {
			sysMenuInfos = this.sysMenuInfoService.getSysMenuInfoByUserId(sysUser.getUserId());
//			每个登录用户默认赋于ROLE_ALL
			SysMenuInfo sysMenuInfo = new SysMenuInfo();
			sysMenuInfo.setMenuCode("ROLE_ALL");
			sysMenuInfo.setAliveFlag(GlobalConstants.FLAG_ENABLE);
			sysMenuInfo.setMenuName("登录用户默认赋于ROLE_ALL");
			sysMenuInfos.add(sysMenuInfo);
		} catch (Exception e) {
			logger.error("获取用户权限", e);
			throw new UsernameNotFoundException(this.messageSource.getMessage("LOGON_USER_PERMISSION_FAIL", null, "获取用户权限失败", null));
		}
		sysUser.setSysMenuInfos(sysMenuInfos);
		return sysUser;
	}

}
